#!/usr/bin/env python.
# -*- coding: utf-8 -*-
import wx
import os

from core import Core
from core.event.mouseevent import MouseEvent
import core.ui.mainframe as auipane
from core.ui.toolsbar import ToolsBarManager
from localobj import Project
from core.ide.commandmanager import CommandManager
from core.ide.langsmanager import LangsManager
from core.ide.datamangager import DataMangager


from ui import Panel
from ui.property import PropertyPanel
from ui.finder import Finder

wildcard = "Druid Project |*.druidxprj; *.druidzprj| Druid XML Project |*.druidxprj;| Druid Zip Project |*.druidzprj;"

class Extend():
    
    def __init__(self):
        self.id="project"
        self.name="Project"
        self.win=None
        CommandManager().listener("runProject",self.onRun)
        CommandManager().listener("project/properties",self.onOpenProperty)
        self.property=None
        
        
    def enable(self):
        Core().getMenu().quickMenu(3,"Project")
        Core().getMenu().quickMenu("Project","New Project")
        Core().getMenu().quickMenu("Project","Open Project").setCommand('select_project')
        Core().getMenu().quickMenu("Project","Close Project")
        Core().getMenu().quickMenu("Project","|tool", "Finder").setCommand('open_finder')
        Core().getMenu().quickMenu("Project","|tool", "Finder").setKeyListner("CTRL+R")
        Core().getMenu().quickMenu("File","New",1,"Project")
        
        DataMangager().getData("Project","Open Last Project",True)
        self.ui=Panel()
        Core().getMainFrame().addPane(self.ui,auipane.PANE_LEFTPANEL)
        
        #self.project=Project("../_project/python/project2.druidxprj")
        #self.openProject("../_project/as3/AS3Project.druidxprj")
        self.openProject("../druid/druid.druidxprj")
        #self.openProject("../_project/flash/Card/card.druidxprj")
        #self.ui.openProject(self.project)
        
        tb=ToolsBarManager().createToolsBar("Launcher","build","play")
        tb.quickToolBar("play").setCommand("runProject")
        tb.quickToolBar("build").setCommand("buildProject")
        choice = wx.ComboBox(tb, -1,"Debug", choices=["Debug","Release"], style=wx.CB_READONLY)
        tb.quickToolBar(choice)
        
        CommandManager().listener("select_project",self.selectProject)
        CommandManager().listener("open_project",self.openProject)
        CommandManager().listener("get_projectType",self.getProjectType)
        CommandManager().listener("open_finder",self.openFinder)
        
    def openFinder(self):
         self._finder=Finder()
         self._finder.Show()
        
    def getProjectType(self):
        return self.project.getProjectType()
    
    def getDefaultProject(self):
        return self.project
        
    def openProject(self,path):
        self.project=Project(path)
        DataMangager().addPriorDataTool(self.project.getDataObject())
        self.ui.openProject(self.project)
        
    def selectProject(self):
        dlg = wx.FileDialog(
            Core().getCentralPanel(), message="Open file",
            defaultDir=os.getcwd(), 
            defaultFile="",
            wildcard=wildcard,
            style=wx.OPEN | wx.CHANGE_DIR
            )

        if dlg.ShowModal() == wx.ID_OK:
            paths = dlg.GetPaths()
            CommandManager().execute("open_project", paths[0]);
            #self.panel=Panel(Core().getCentralPanel())
            #self.panel.openFile(paths[0])
            #self.panel.setText(open(paths[0],'r').read(),LangsManager().getLangFromFile(paths[0]))

        dlg.Destroy()    
        
    def onRun(self):
        path=self.project.getDocumentRoot()
        path=os.path.join(self.project.getBaseDir(),path)
        path=os.path.abspath(path)
        lang=LangsManager().getLangFromFile(path)
        lang.run(path,"Release",self.project.getBaseDir())
    
    def onOpenProperty(self,project):
        if self.property:
            self.property.Close()
            del self.property
            
        self.property=PropertyPanel(project)
        self.property.Show(True)
    
    def disable(self):
        pass
        #del self.win  

